package com.suma.platform.core.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

import com.nfsq.platform.utils.common.RequestUtils;

/**
 * @author suma
 * @date 2018/1/10 上午9:21
 */
public class ApiAuthFilter extends OncePerRequestFilter {

	private static final long TIMEOUT = 1800000;

	private static final String TEST_PARAM = "1";
	private static final String APP_KEY = "appKey";
	private static final String VERSION = "version";
	private static final String SIGN = "sign";
	private static final String TIMESTAMP = "timestamp";
	private static final String TOKEN = "token";

	private static final String TOKEN_TEST = "123";
	private static final String SIGN_TEST = "123";

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
		// 测试
		if (TEST_PARAM.equals(RequestUtils.getValue(request, "t"))) {
			filterChain.doFilter(request, response);
		} else {
			String appKey = request.getParameter(APP_KEY);
			String version = request.getParameter(VERSION);
			String sign = request.getParameter(SIGN);
			String timestamp = request.getParameter(TIMESTAMP);
			String token = request.getParameter(TOKEN);

			if (RequestUtils.isEmpty(request, APP_KEY, VERSION, SIGN, TIMESTAMP, TOKEN)) {
				// 公共参数缺失
				logger.info("PARAMETERS_MISSING");
			} else {
				filterChain.doFilter(request, response);
			}
		}
	}
}
